package com.zbcz.erp.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.erp.ErpApplication
import com.zbcz.erp.bean.ApplyHistoryBean
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.network.ErpApi
import com.zbcz.erp.network.RetrofitManager
import com.zbcz.erp.sp.SharedPreferencesManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MyApplyHistoryViewModel : ViewModel() {

    var mHistoryLiveData: MutableLiveData<List<ApplyHistoryBean?>?> = MutableLiveData()
    private val mApi = RetrofitManager.getRetrofit().create(ErpApi::class.java)

    fun getHistoryFlow(id: String) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = getMyHistoryList(id)
                mHistoryLiveData.postValue(result)
            }
        }
    }


    private suspend fun getMyHistoryList(
        id: String
    ): List<ApplyHistoryBean?>? {
        try {
            val result = mApi?.getHistoricFlow(
                id,
                SharedPreferencesManager.getToken(ErpApplication.mApplication)
            )
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}